﻿package classes.store 
{  
	import flash.display.Sprite;
	import flash.net.URLLoader;
	import flash.net.URLRequest; 
	import flash.events.Event;
	import flash.events.MouseEvent; 
	
	import com.adobe.serialization.json.JSON; 
	import com.mttang.event.JEvent;
	import com.mttang.event.CommonEvent;
	import classes.store.storeclasses.ShopList;
	import classes.store.storeclasses.Knapsack;
	import classes.store.storeclasses.TransactionsGoods;
	import classes.store.storeclasses.StoreFile;
	
	import com.mttang.common.InterfaceCommon;
	
	/**
	 * 商店系统主类
	 * @author rock 
	 */
	public class StoreRockman extends Sprite implements InterfaceCommon
	{
		public var shopList:ShopList;//商品类实例
		public var trnGoods:TransactionsGoods;//交易处理类
		public var knapsack:Knapsack;//	背包类实例
		public var storeJson:Array = [];//保存商店数据
		public static var _saveData:Object;
		 
		private var _uUrlLoader:URLLoader;
		
		public function StoreRockman():void
		{ 
		    //setData(new Object());	
		} 
		
		//所有场景可以暂停/播放
		public function rock_pause():void
		{ 
		    buyBtn.removeEventListener(MouseEvent.CLICK, buy_CkHandler)//买  
			deleteBtn.removeEventListener(MouseEvent.CLICK, deleteBtn_CkHandler)//买	
			shopList.removeEventListener(CommonEvent.GOODS_CLICK, shopList_ckHandler);
			trnGoods.removeEventListener(CommonEvent.GOODS_BUY_OK, trnGoods_okHandler);
			trnGoods.removeEventListener(CommonEvent.GOODS_BUY_CENCEL, trnGoods_cencelHandler);
		}
		public function rock_play():void
		{
		    buyBtn.addEventListener(MouseEvent.CLICK, buy_CkHandler)//买  
			deleteBtn.addEventListener(MouseEvent.CLICK, deleteBtn_CkHandler)//买	
			shopList.removeEventListener(CommonEvent.GOODS_CLICK, shopList_ckHandler);
			trnGoods.removeEventListener(CommonEvent.GOODS_BUY_OK, trnGoods_okHandler);
			trnGoods.removeEventListener(CommonEvent.GOODS_BUY_CENCEL, trnGoods_cencelHandler);
		}
		//关闭清除当前的场景;swf文件的声音，flv在这里清除
		public function rock_close():void
		{  
			shopList.cleanData(); 
			shopList.shopJsonArr=[];
			knapsack.clearData(); 
			knapsack.knapsackArr=[];
		}
		//所有的场景通过这个接口获取数据对象
		public function setData(_data:Object):void
		{
			_saveData = _data;
		    init();	
		}
		//控制声音
		public function cfgSound(status:String):void
		{}
		
		/*
		//加载数据json
		public function init():void
		{  
			showShop();
			
			_uUrlLoader=new URLLoader();
			_uUrlLoader.load(new URLRequest(_sJsonUrl));
			_uUrlLoader.addEventListener(Event.COMPLETE,decodeJSON);  
		}
		 
		private function decodeJSON(e:Event):void
		{ 
		   storeJson=JSON.decode(URLLoader(e.target).data).Goods;
		   showShop();  
		}
		*/
		
		private function init():void
		{ 
			shopList=new ShopList(StoreFile.storeJson);//商品显示类
			addChild(shopList);
			shopList.initData(); 
			shopList.addEventListener(CommonEvent.GOODS_CLICK, shopList_ckHandler);
		 
			knapsack = new Knapsack();//背包类
			addChild(knapsack);
			
			trnGoods = new TransactionsGoods();//交易处理类
			addChild(trnGoods); 
			 
		    buyBtn.addEventListener(MouseEvent.CLICK, buy_CkHandler)//买  
			deleteBtn.addEventListener(MouseEvent.CLICK, deleteBtn_CkHandler)//删除  
			shopCloseBtn.addEventListener(MouseEvent.CLICK, closeBtn_CkHandler)//关闭 
		} 
		//商品物品时提示购买
		private function shopList_ckHandler(e:JEvent):void
		{  
			if (knapsack.knapsackArr.length < 6)//判断收藏夹是否已满
			{
			    knapsack.favorite();//收藏 到临时 背包
			    knapsack.show();//显示临时背包物品 
			}
			else
			{
			    trnGoods.showShopListAlert();	
			}
		}
		//收藏夹购买按钮
		private function buy_CkHandler(e:MouseEvent):void 
		{trace("购买！！！！！！！！")
			trnGoods.process(knapsack.knapsackArr);	 
			trnGoods.addEventListener(CommonEvent.GOODS_BUY_OK, trnGoods_okHandler);
			trnGoods.addEventListener(CommonEvent.GOODS_BUY_CENCEL, trnGoods_cencelHandler);  
		}
		//收藏夹删除按钮 
		private function deleteBtn_CkHandler(e:MouseEvent):void
		{
			knapsack.deleteGoods();
		}
		//商店关闭按钮
		private function closeBtn_CkHandler(e:MouseEvent):void
		{
		    dispatchEvent(new Event(CommonEvent.BACK_EVENT));	
		}
		//确认购买
		private function trnGoods_okHandler(e:JEvent):void
		{
		    trnGoods.cleanData();//清理交易处理类数据 
			knapsack.buyGoods();   
		}
		//取消购买
		private function trnGoods_cencelHandler(e:JEvent):void
		{
			trnGoods.cleanData();	
		}  
		 
	}
	
}